----------------------------
-- Simplistic Street
-- by Jammie Dodger
--
-- Project Started - October 20, 2009
-- Completion -None
----------------------------

local MalePainSoundsLight = {
Sound("vo/npc/male01/ow01.wav"),
Sound("vo/npc/male01/ow02.wav"),
Sound("vo/npc/male01/pain01.wav"),
Sound("vo/npc/male01/pain02.wav"),
Sound("vo/npc/male01/pain03.wav")
}

local MalePainSoundsMed = {
Sound("vo/npc/male01/pain04.wav"),
Sound("vo/npc/male01/pain05.wav"),
Sound("vo/npc/male01/pain06.wav")
}

local MalePainSoundsHeavy = {
Sound("vo/npc/male01/pain07.wav"),
Sound("vo/npc/male01/pain08.wav"),
Sound("vo/npc/male01/pain09.wav")
}

local MaleDeathSounds = {
Sound("vo/npc/male01/no02.wav"),
Sound("vo/npc/Barney/ba_ohshit03.wav"),
Sound("vo/npc/Barney/ba_ohshit03.wav"),
Sound("vo/npc/Barney/ba_no01.wav"),
Sound("vo/npc/Barney/ba_no02.wav")
}

local FemalePainSoundsLight = {
Sound("vo/npc/female01/pain01.wav"),
Sound("vo/npc/female01/pain02.wav"),
Sound("vo/npc/female01/pain03.wav")
}

local FemalePainSoundsMed = {
Sound("vo/npc/female01/pain04.wav"),
Sound("vo/npc/female01/pain05.wav"),
Sound("vo/npc/female01/pain06.wav")
}

local FemalePainSoundsHeavy = {
Sound("vo/npc/female01/pain07.wav"),
Sound("vo/npc/female01/pain08.wav"),
Sound("vo/npc/female01/pain09.wav")
}

local FemaleDeathSounds = {
Sound("vo/npc/female01/no01.wav"),
Sound("vo/npc/female01/ow01.wav"),
Sound("vo/npc/female01/ow02.wav")
}

local CombinePainSounds = {
Sound("npc/combine_soldier/pain1.wav"),
Sound("npc/combine_soldier/pain2.wav"),
Sound("npc/combine_soldier/pain3.wav")
}

local CombineDeathSounds = {
Sound("npc/combine_soldier/die1.wav"),
Sound("npc/combine_soldier/die2.wav"),
Sound("npc/combine_soldier/die3.wav")
}

local MetroPainSounds = {
"npc/metropolice/pain1.wav",
"npc/metropolice/pain2.wav",
"npc/metropolice/pain3.wav",
"npc/metropolice/pain4.wav"
}

local MetroDeathSounds = {
"npc/metropolice/die1.wav",
"npc/metropolice/die2.wav",
"npc/metropolice/die3.wav",
"npc/metropolice/die4.wav"
}

local GrigoryPainSounds = {
"vo/ravenholm/monk_pain01.wav",
"vo/ravenholm/monk_pain02.wav",
"vo/ravenholm/monk_pain03.wav",
"vo/ravenholm/monk_pain04.wav",
"vo/ravenholm/monk_pain05.wav",
"vo/ravenholm/monk_pain06.wav",
"vo/ravenholm/monk_pain07.wav",
"vo/ravenholm/monk_pain08.wav",
"vo/ravenholm/monk_pain09.wav",
"vo/ravenholm/monk_pain10.wav",
"vo/ravenholm/monk_pain12.wav"
}

local GrigoryDeathSounds = {
"vo/ravenholm/monk_death07.wav"
}

local MaleModels = {
"Barney",
"Kleiner",
"Mag",
"Eli",
"Man",
"Breen",
"Male",
"Pheonix",
"Leet",
"Stripped",
"Hostage",
"Gas",
"Arctic",
"Urban"
}

local FemaleModels = {
"Alyx",
"Moss",
"Female"
}

local CombineModels = {
"Combine",
"Prison",
"Super"
}

local CitizenSpawn = {
"models/player/Group01/Male_01.mdl",
"models/player/Group01/male_02.mdl",
"models/player/Group01/male_03.mdl",
"models/player/Group01/male_04.mdl",
"models/player/Group01/male_05.mdl",
"models/player/Group01/male_06.mdl",
"models/player/Group01/male_07.mdl",
"models/player/Group01/male_08.mdl",
"models/player/Group01/male_09.mdl",
"models/player/Group01/female_01.mdl",
"models/player/Group01/female_02.mdl",
"models/player/Group01/female_03.mdl",
"models/player/Group01/female_04.mdl",
"models/player/Group01/female_06.mdl",
"models/player/Group01/female_07.mdl"
}

local CopSpawn = {
"models/player/police.mdl"
}

function _R.Player:FindGender()
	for k,v in pairs(FemaleModels) do
		if string.find(string.lower(self:GetModel()), string.lower(v)) then
			self:SetNWString("Gender", "Female")
			return
		end
	end
	for k,v in pairs(MaleModels) do
		if string.find(string.lower(self:GetModel()), string.lower(v)) then
			self:SetNWString("Gender", "Male")
			return
		end
	end
	
	for k,v in pairs(CombineModels) do
		if string.find(string.lower(self:GetModel()), string.lower(v)) then
			self:SetNWString("Gender", "Combine")
			return
		end
	end
	
		if string.find(string.lower(self:GetModel()), "metro") then
			self:SetNWString("Gender", "Metro")
			return
		end	
		
		if string.find(string.lower(self:GetModel()), "grig") then
			self:SetNWString("Gender", "Grigory")
			return
		end			
	
	self:SetNWString("Gender", "Unknown")
	
	return
end

function GM:PlayerSpawn( ply )

	timer.Simple(0.1, function() ply:FindGender() end)
	ply.IsCrippled = false
	
	hook.Call( "PlayerSetModel", GAMEMODE, ply )
	hook.Call( "PlayerLoadout", GAMEMODE, ply )
	
end

function GM:PlayerSetModel( ply )
	if ply:Team() == 1 then 
		ply:SetModel(table.Random(CitizenSpawn)) 
	else 
		ply:SetModel(table.Random(CopSpawn)) 
	end	
end

function GM:PlayerInitialSpawn( ply )

	ply:Load()
	ply:SetTeam(1)
	ply:CheckPrivilages()
	
end

function GM:PlayerAuthed(ply, SteamID, UniqueID)
	if timer.IsTimer("DisconnectTimer"..UniqueID) then
		timer.Destroy("DisconnectTimer"..UniqueID)
	end
end

function GM:PlayerLoadout( ply )
	if ply:Team() == 2 then
		ply:Give("ss_keys")
		ply:Give("weapon_physgun")
		ply:Give("weapon_physcannon")
		ply:Give("gmod_tool")
		ply:Give("ss_arrest")
	elseif ply:Team() == 3 then
		ply:Give("ss_keys")
		ply:Give("weapon_physgun")
		ply:Give("weapon_physcannon")
		ply:Give("gmod_tool")
	else
		ply:Give("ss_keys")
		ply:Give("weapon_physgun")
		ply:Give("weapon_physcannon")
		ply:Give("gmod_tool")
	end
end
	
function DisconnectPlayer( ply )

local PlayerUnique = ply:UniqueID()

timer.Create("DisconnectTimer"..ply:UniqueID(), 120, 1, function()
for k,v in pairs(ents.GetAll()) do
	if v:IsDoor() then
	
		if v.Owners == PlayerUnique then
			RemoveFromDoor(v)
		end
		
	if v.CoOwners and table.HasValue(v.CoOwners, PlayerUnique) then
		table.remove(v.CoOwners, PlayerUnique)
			end
		end
	end
end)

end

hook.Add( "PlayerDisconnected", "DisconnectPlayer", DisconnectPlayer )

function GM:ShutDown()

	SaveDoors()
	
end

function GM:PlayerGiveSWEP( ply, class, wep )
	if ( ply:IsAdmin() or ply:IsSuperAdmin() ) then
		return true
	else
		return false
	end
end

function GM:PlayerSpawnSWEP( ply, class, wep )
	if ( ply:IsAdmin() or ply:IsSuperAdmin() ) then
		return true
	else
		return false
	end
end

function GM:PlayerSpawnSENT( ply, name )
	if ( ply:IsAdmin() or ply:IsSuperAdmin() ) then
		return true
	else
		return false
	end
end

function GM:PlayerSpawnRagdoll( ply, mdl )
	if ( ply:IsAdmin() or ply:IsSuperAdmin() ) then
		return true
	else
		return false
	end
end

function GM:PlayerSpawnEffect( ply, mdl )
	if ( ply:IsAdmin() or ply:IsSuperAdmin() ) then
		return true
	else
		return false
	end
end

function GM:PlayerSpawnVehicle( ply, mdl )
	if ( ply:IsAdmin() or ply:IsSuperAdmin() ) then
		return true
	else
		return false
	end
end

function GM:PlayerSpawnNPC( ply, npc, weapon )
	if ( ply:IsAdmin() or ply:IsSuperAdmin() ) then
		return true
	else
		return false
	end
end
 
function GM:PlayerSpawnProp( ply, mdl )

	if( IsBannedModel( mdl )) then
		ply:SendNotify( "This prop is banned", NOTIFY_ERROR, 4 );
		return false;
	end

	return true;

end

function GM:EntityTakeDamage( ent, inflictor, attacker, amount, dmginfo )
	
	if ent:IsPlayer() then
		ent:PlayPainSound()
	end
	
end

function GM:PlayerFootstep( ply, pos, foot, sound, volume, rf )  

       local ft = math.random( 1, 3 );	
	   
       ply:EmitSound( Sound( "player/footsteps/tile"..ft..".wav" ), 35, 100 );	
	   
end 

function GM:CanPlayerSuicide( ply ) 

	chat.AddText(ply, Color(255, 0, 0, 255), "You cannot suicide! Contact an admin." )

	return false
	
end

function GM:CanTool( ply, tr, toolmode )
	if ( toolmode == "duplicator" ) then
		if tr.Entity:IsDoor() then
			return false
		end
	end
	
	return true
end

function GM:PlayerSwitchFlashlight( pl )
	
	pl.lastFlash = pl.lastFlash or CurTime()
	if CurTime() < pl.lastFlash then return false end
	pl.lastFlash = CurTime() + 1
	return true
	
end

function _R.Player:PlayDeathSound()
	if self:GetNWString("Gender") == "Female" then
		self:EmitSound(table.Random(FemaleDeathSounds))
	elseif self:GetNWString("Gender") == "Male" then
		self:EmitSound(table.Random(MaleDeathSounds))
	elseif self:GetNWString("Gender") == "Combine" then
		self:EmitSound(table.Random(CombineDeathSounds))
	elseif self:GetNWString("Gender") == "Metro" then
		self:EmitSound(table.Random(MetroDeathSounds))	
	elseif self:GetNWString("Gender") == "Grigory" then
		self:EmitSound(table.Random(GrigoryDeathSounds))			
	end
end

function _R.Player:IsModerator()
	return self:IsUserGroup("moderator")
end

function GM:PlayerDeathSound()
	return true
end

function GM:PlayerDeath( Victim, Inflictor, Attacker )
	self.BaseClass:PlayerDeath( Victim, Inflictor, Attacker )
	Victim:PlayDeathSound()
	Victim.IsCrippled = false
end

function GM:Think()
	for k, v in pairs( player.GetAll() ) do
		v:AFK()
		if v:IsAFK() then
			
			v:Kick("Kicked for inactivity")
			
		end		
	end
end

function GM:KeyPress( pl, key )
	if key then
		pl:AddAFK()
	end
end

function GM:PlayerConnect( name, address ) 
    return true
end 

function GM:ScalePlayerDamage ( Player, HitGroup, DmgInfo )

		if (HitGroup == HITGROUP_RIGHTLEG or HitGroup == HITGROUP_LEFTLEG) and Player:Alive() and !Player.IsCrippled then
			Player.IsCrippled = true
			Player:SendNotify("You've broken your legs!", NOTIFY_ERROR, 4)
			self:SetPlayerSpeed(Player, 100, 100)
		end
	
	return DmgInfo
end

function _R.Player:PlayPainSound()

	self.NextPainSound = self.NextPainSound or CurTime()
	if CurTime() < self.NextPainSound then return end

	self.NextPainSound = CurTime() + 0.2
	
	local health = self:Health()

	if self:GetNWString("Gender") == "Female" then
		if health > 68 then
			self:EmitSound(table.Random(FemalePainSoundsLight))
		elseif health > 36 then
			self:EmitSound(table.Random(FemalePainSoundsMed))
		else
			self:EmitSound(table.Random(FemalePainSoundsHeavy))
		end
	
	elseif self:GetNWString("Gender") == "Male" then
		if health > 68 then
			self:EmitSound(table.Random(MalePainSoundsLight))
		elseif health > 36 then
			self:EmitSound(table.Random(MalePainSoundsMed))
		else
			self:EmitSound(table.Random(MalePainSoundsHeavy))
		end
		
	elseif self:GetNWString("Gender") == "Combine" then
			self:EmitSound(table.Random(CombinePainSounds))
	
	elseif self:GetNWString("Gender") == "Metro" then
			self:EmitSound(table.Random(MetroPainSounds))
			
	elseif self:GetNWString("Gender") == "Grigory" then
			self:EmitSound(table.Random(GrigoryPainSounds))	
	end
end

function _R.Player:SendNotify(msg,type,len)
	umsg.Start("sendnotify",self)
		umsg.String(msg)
		umsg.Short(type)
		umsg.Short(len)
	umsg.End()
end